package com.littcore.saap.assistant.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.eventbus.EventBus;
import com.littcore.dao.IResultsetTransformer;
import com.littcore.dao.page.IPageList;
import com.littcore.dao.ql.PageParam;
import com.littcore.eventbus.IEventBus;
import com.littcore.saap.assistant.dao.UserCommentDao;
import com.littcore.saap.assistant.event.UserCommentEvent;
import com.littcore.saap.assistant.po.UserComment;
import com.littcore.saap.assistant.service.IUserCommentService;
import com.littcore.saap.assistant.vo.UserCommentVo;
import com.littcore.saap.assistant.webservice.IUserCommentWebService;
import com.littcore.saap.common.vo.IUserInfo;
import com.littcore.saap.core.web.util.LoginUtils;
import com.littcore.saap.system.service.IUserInfoService;
import com.littcore.util.BeanCopier;

/**
 * 
 * Comment service impl.
 * <pre><b>Description：</b>
 *    User Coomment
 * </pre>
 * 
 * @author <a href="mailto:littcai@hotmail.com">Bob.cai</a>
 * @since 2013-11-19
 * @version 1.0
 */
public class UserCommentServiceImpl implements IUserCommentService, IUserCommentWebService
{ 
	/** Logger. */
  private static final Logger logger = LoggerFactory.getLogger(UserCommentServiceImpl.class);
    
  @Resource
  private UserCommentDao userCommentDao;
  @Resource
  private IUserInfoService userInfoService;
  @Resource
  private IEventBus eventBus;
    
  /* (non-Javadoc)
   * @see com.littcore.saap.assistant.service.impl.IUserCommentWebService#save(int, int, java.lang.String, int, java.lang.String)
   */
  @Override
  public UserCommentVo save(int tenantId, int userId, String moduleCode, int dataId, String content)
  {
    UserComment userComment = new UserComment();
    userComment.setTenantId(tenantId);
    userComment.setModuleCode(moduleCode);
    userComment.setRecordId(dataId);
    userComment.setContent(content);
    userComment.setCreateBy(userId);
    IUserInfo userInfo = userInfoService.find(userId);
    userComment.setCreateUsername(userInfo.getUserName()); 
    userComment.setCreateDatetime(new Date());
    return this.save(userComment);
  }

   	/**
	 * Save.
	 * @param userComment UserComment
	 * @return id
	 */
	public UserCommentVo save(UserComment userComment)
	{
	  userComment.setCreateDatetime(new Date());
		userCommentDao.save(userComment);
		UserCommentVo userCommentVo = BeanCopier.copy(userComment, UserCommentVo.class);
		IUserInfo userInfo = userInfoService.find(userCommentVo.getCreateBy());
		userCommentVo.setCreateUser(userInfo);
    
    //发布事件
    {
      UserCommentEvent event = new UserCommentEvent();
      event.setModuleCode(userComment.getModuleCode());
      event.setFuncCode("addComment");
      event.setRecordId(userComment.getRecordId());
      event.setTenantId(userComment.getTenantId());
      event.setOpBy(userComment.getCreateBy());
      event.setOpDatetime(userComment.getCreateDatetime());
      event.addData("commentId", userComment.getId());
      event.setOpComment(userComment.getContent());
      eventBus.publish(event);
    }
		return userCommentVo;
	}
	
   	/**
	 * Update.
	 * @param userComment UserComment
	 */
	public void update(UserComment userComment)
	{
		userCommentDao.update(userComment);
	}			
   
   	/* (non-Javadoc)
     * @see com.littcore.saap.assistant.service.impl.IUserCommentWebService#delete(java.lang.Integer)
     */
	@Override
  public void delete(Integer id)
	{
	  UserComment userComment = this.load(id);
		this.delete(userComment);		
	}
	
	/**
	 * Delete by instance.
	 * @param id id
	 */
	public void delete(UserComment userComment)
	{
		userCommentDao.delete(userComment);
	  //发布事件
    {
      UserCommentEvent event = new UserCommentEvent();
      event.setModuleCode(userComment.getModuleCode());
      event.setFuncCode("delComment");
      event.setRecordId(userComment.getRecordId());
      event.setTenantId(userComment.getTenantId());
      event.setOpBy(LoginUtils.getLoginOpId().intValue());
      event.setOpDatetime(new Date());
      event.addData("commentId", userComment.getId());
      event.setOpComment(userComment.getContent());
      eventBus.publish(event);
    }
	}
	
	/**
	 * 删除业务记录的所有评论.
	 *
	 * @param moduleCode the module code
	 * @param recordId the record id
	 */
	public void deleteByRecord(String moduleCode, Integer recordId)
  {
    String hql = "delete from UserComment where moduleCode=? and recordId=?";  
    userCommentDao.execute(hql, new Object[]{moduleCode, recordId});
  }
	
	/**
	 * Load by id.
	 * @param id id
	 * @return UserComment
	 */
	public UserComment load(Integer id)
	{
		return userCommentDao.load(id);
	}
	
	/**
	 * list by page.
	 * 
	 * @param pageParam params
	 * @return IPageList IPageList
	 */
	public IPageList listPage(PageParam pageParam)
	{
		String listHql = "select obj from UserComment obj"
			+ "-- and obj.tenantId={tenantId}"
			+ "-- and obj.moduleCode={moduleCode}"
			+ "-- and obj.recordId={recordId}"
			;	
		return userCommentDao.listPage(listHql, pageParam);
	}
	
	/* (non-Javadoc)
   * @see com.littcore.saap.assistant.service.impl.IUserCommentWebService#findPage(com.littcore.dao.ql.PageParam)
   */
	@Override
  public IPageList findPage(PageParam pageParam)
	{
	  IPageList pageList = this.listPage(pageParam);
	  pageList.setResultsetTransformer(new IResultsetTransformer(){

	    @Override
      public List transform(List srcList)
      {
        List<UserCommentVo> voList = BeanCopier.copyList(srcList, UserCommentVo.class);
        for (UserCommentVo userCommentVo : voList)
        {
          IUserInfo userInfo = userInfoService.find(userCommentVo.getCreateBy());
          userCommentVo.setCreateUser(userInfo);
        }
        return voList;
      }
	    
	  });
	  return pageList;
	}
}